﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Reflection;
using Microsoft.SharePoint.Publishing;

namespace Inline.SharePoint.ExtendedVariations.Helpers
{

    class VariationHandler
    {

        /// <summary>
        /// Gets URL of the root variation web via reflection. This is the parent web of the variation sites themselves.
        /// </summary>
        /// <param name="currentUrl">The URL you start from</param>
        /// <returns>The URL of the root variation web</returns>
        public string GetVariationRootUrl(string currentUrl)
        {

            using (SPSite site = new SPSite(currentUrl))
            {

                // Get Assembly hosting the hidden VariationSettings class 
                Assembly a = typeof(PublishingWeb).Assembly;

                Type[] types = a.GetTypes();
                Type VariationSettingsType = null;

                // Find VariationSettings type
                foreach (Type t in types)
                {
                    if (t.FullName == "Microsoft.SharePoint.Publishing.Internal.VariationSettings")
                    {
                        VariationSettingsType = t;
                        break;
                    }
                }

                // Instantiate a VariationSettings object 
                ConstructorInfo ci = VariationSettingsType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
                                           null, new Type[] { typeof(SPSite), typeof(bool) }, null);
                object VariationSettings = ci.Invoke(new object[] { site, false });

                // Retrieve the URL of the variation root
                PropertyInfo pi = VariationSettings.GetType().GetProperty("RootPublishingWebUrl",
                                           BindingFlags.NonPublic | BindingFlags.Instance);
                string Url = pi.GetValue(VariationSettings, null) as string;
                
                return Url;

            }

        }

        /// <summary>
        /// Gets the source variation label via reflection
        /// </summary>
        /// <param name="currentUrl">The URL you start from</param>
        /// <returns>The source variation label</returns>
        public VariationLabel GetSourceVariationLabel(string currentUrl)
        {

            using (SPSite site = new SPSite(currentUrl))
            {

                // Get Assembly hosting the hidden VariationSettings class 
                Assembly a = typeof(PublishingWeb).Assembly;

                Type[] types = a.GetTypes();
                Type VariationSettingsType = null;

                // Find VariationSettings type
                foreach (Type t in types)
                {
                    if (t.FullName == "Microsoft.SharePoint.Publishing.Internal.VariationSettings")
                    {
                        VariationSettingsType = t;
                        break;
                    }
                }

                // Instantiate a VariationSettings object 
                ConstructorInfo ci = VariationSettingsType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
                                           null, new Type[] { typeof(SPSite), typeof(bool) }, null);
                object VariationSettings = ci.Invoke(new object[] { site, false });

                // Retrieve the source variation label
                PropertyInfo pi = VariationSettings.GetType().GetProperty("SourceLabel",
                                           BindingFlags.NonPublic | BindingFlags.Instance);
                VariationLabel vLabel = pi.GetValue(VariationSettings, null) as VariationLabel;

                return vLabel;

            }

        }

        /// <summary>
        /// Gets the root variation web via reflection. This is the parent web of the variation sites themselves.
        /// </summary>
        /// <param name="currentUrl">The URL you start from</param>
        /// <returns>The root variation web</returns>
        public PublishingWeb GetVariationRootPublishingWeb(string currentUrl)
        {

            using (SPSite site = new SPSite(currentUrl))
            {

                // Get Assembly hosting the hidden VariationSettings class 
                Assembly a = typeof(PublishingWeb).Assembly;

                Type[] types = a.GetTypes();
                Type VariationSettingsType = null;

                // Find VariationSettings type
                foreach (Type t in types)
                {
                    if (t.FullName == "Microsoft.SharePoint.Publishing.Internal.VariationSettings")
                    {
                        VariationSettingsType = t;
                        break;
                    }
                }

                // Instantiate a VariationSettings object 
                ConstructorInfo ci = VariationSettingsType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
                                           null, new Type[] { typeof(SPSite), typeof(bool) }, null);
                object VariationSettings = ci.Invoke(new object[] { site, false });

                // Retrieve the URL of the variation root
                PropertyInfo pi = VariationSettings.GetType().GetProperty("RootPublishingWeb",
                                       BindingFlags.NonPublic | BindingFlags.Instance);
                PublishingWeb pubWeb = pi.GetValue(VariationSettings, null) as PublishingWeb;

                return pubWeb;

            }

        }

    }

}